﻿using BLL;
using BLL.Access;
using Platform.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;
using Web.Class;

namespace Web.Pages.QuShui.DiXiaS._8.QiTaGL
{
    public partial class DiDiaoYuanAndPaiShui : BasePage
    {

        /// <summary>
        /// 保存结果集1
        /// </summary>
        private DataTable dtGrid
        {
            get
            {
                return ViewState["dtGrid"] as DataTable;
            }
            set
            {
                ViewState["dtGrid"] = value;
            }
        }

        /// <summary>
        /// 保存结果集1
        /// </summary>
        private DataTable dtGrid2
        {
            get
            {
                return ViewState["dtGrid2"] as DataTable;
            }
            set
            {
                ViewState["dtGrid2"] = value;
            }
        }

       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) 
            { 
                InitDate();
                GetData();
            }
            //ScriptManager1.RegisterPostBackControl(btn_ToExDDY);
            ScriptManager1.RegisterPostBackControl(btnExport);
        }


        private void InitDate() 
        {
            int wtype = BusinessTool.WType;
            tab1txtShuiLiang.Text = BusinessTool.GetSLYMorZWNY(1, wtype).ToString();
            tab2txtShuiLiang.Text = BusinessTool.GetSLYMorZWNY(1, wtype).ToString();
        }

        protected void btnChaXun_Click1(object sender, EventArgs e)
        {
            GetData();
        }

        public void GetData()
        {
            int wtype = BusinessTool.WType;
            string i_y = "", i_m = "";
            //SearchSpHelper sphelper=new SpHelper();
            if (!string.IsNullOrEmpty(tab1txtShuiLiang.Text))
                i_y = tab1txtShuiLiang.Text.Trim().Substring(0, 4);
            if (!string.IsNullOrEmpty(tab1txtShuiLiang.Text))
                i_m = tab1txtShuiLiang.Text.Trim().Substring(4, 2);
            this.dtGrid = SearchSpHelper.P_QR_DDY(i_y, i_m, wtype).Tables[0];
            gvDiDiaoYuan.DataSource = this.dtGrid;
            gvDiDiaoYuan.DataBind();
            int hj1 = 0, hj2 = 0, hj3 = 0, hj4 = 0;
            if (this.dtGrid.Rows.Count>0)
            {
                for (int i = 0; i < this.dtGrid.Rows.Count; i++)
                {
                    hj1 += this.dtGrid.Rows[i]["普通采"].ToString() != "" ? Convert.ToInt32(this.dtGrid.Rows[i]["普通采"]) : 0;
                    hj2 += this.dtGrid.Rows[i]["矿泉采"].ToString() != "" ? Convert.ToInt32(this.dtGrid.Rows[i]["矿泉采"]) : 0;
                    hj3 += this.dtGrid.Rows[i]["采灌采"].ToString() != "" ? Convert.ToInt32(this.dtGrid.Rows[i]["采灌采"]) : 0;
                    hj4 += this.dtGrid.Rows[i]["回灌"].ToString() != "" ? Convert.ToInt32(this.dtGrid.Rows[i]["回灌"]) : 0;
                }
                zjl.Text = "共" + this.dtGrid.Rows.Count.ToString() + "条";
            }
            else
            {
                zjl.Text = "共0条";
            }
           
            ASPxPuTong.Text = hj1.ToString();
            ASPxKuangQuan.Text = hj2.ToString();
            ASPxCaiGuan.Text = hj3.ToString();
            ASPxHuiGuan.Text = hj4.ToString();
        }
        //protected void btn_ToExDDY_Click1(object sender, EventArgs e)
        //{
        //    if (this.dtGrid == null || this.dtGrid.Rows.Count == 0) return;
        //    string sName = "DDY" + this.tab1txtShuiLiang.Text.Substring(2, 4) + ".xls";
        //    if (this.dtGrid.Rows.Count>0)
        //    {
        //        PageTools.CreateExcel(this.dtGrid, sName);
        //    }
        //}

        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// 第二个选项卡查询数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            getData2();
        }
        private void getData2()
        {
            int wtype = BusinessTool.WType;
            int qx = Convert.ToInt32(cmbqx.Value);
            int jd = Convert.ToInt32(cmbjd.Value);
            DateTime dtime = DateTime.Now;
            try
            {
                int i_y =Convert.ToInt32( tab2txtShuiLiang.Text.Trim().Substring(0, 4));
                //int sMonth = tab2txtShuiLiang.Text.Trim().Substring(4, 2);
                this.dtGrid2 = SearchSpHelper.P_QS_DXSBB0601_SC(i_y, qx, jd, dtime).Tables[0];
            
                gvPaiShuiSuo.DataSource = this.dtGrid2;
                gvPaiShuiSuo.DataBind();

                zjl2.Text = "共" + this.dtGrid2.Rows.Count.ToString() + "条";
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            int index = ASPxPageControl1.ActiveTabIndex;
            string sName = "";
            if (index == 0)
            {
                gvDiDiaoYuan.DataSource = dtGrid;
                gvDiDiaoYuan.DataBind();
                ASPxGridViewExporter1.GridViewID = "gvDiDiaoYuan";
                sName = "DDY" + this.tab1txtShuiLiang.Text.Substring(2, 4);
            }
            else if (index == 1)
            {
                gvPaiShuiSuo.DataSource = dtGrid2;
                gvPaiShuiSuo.DataBind();
                ASPxGridViewExporter1.GridViewID = "gvPaiShuiSuo";
                sName = "PSS" + tab1txtShuiLiang.Text.Substring(2, 4);
            }
            //if (this.dtGrid2 == null || this.dtGrid2.Rows.Count == 0) return;
            //string sName = "PSS" + tab1txtShuiLiang.Text.Substring(2,4) + ".xls";
            //if (this.dtGrid2.Rows.Count > 0)
            //{
            //    PageTools.CreateExcel(this.dtGrid2, sName);
            //}

            ASPxGridViewExporter1.WriteXlsToResponse(sName);
        }

      
    }
}